package com.five.welcome.controller;


import cn.hutool.extra.pinyin.PinyinUtil;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.five.welcome.entity.Studentinfo;
import com.five.welcome.entity.statistic.GenderOfStudentCount;
import com.five.welcome.result.Result;
import com.five.welcome.service.DormitoryallocationService;
import com.five.welcome.service.StudentinfoService;
import com.five.welcome.service.WelcomeProgressService;
import com.five.welcome.utils.SNumberUtils;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import org.springframework.stereotype.Controller;

import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
import java.util.List;
@Slf4j
@CrossOrigin
@Tag(name = "学生信息接口")
@RestController
@RequestMapping("/studentinfo")
public class StudentinfoController {

    @Autowired
    private StudentinfoService studentinfoService;

    @Autowired
    private WelcomeProgressService welcomeProgressService;

    @Autowired
    private SNumberUtils sNumberUtils;

    @Autowired
    private DormitoryallocationService dormitoryallocationService;
    //注册学生信息
    @Operation(summary = "注册学生信息")
    @PostMapping("/addStudent")
    public Result addStudent(@RequestBody Studentinfo studentinfo) {
        studentinfo.setName(PinyinUtil.getFirstLetter(studentinfo.getSName(), ""));
        int s = studentinfoService.addStudent(studentinfo);
        if (s > 0) {
            //获取学号
            String sNumber = studentinfo.getSNumber();
            String sName=studentinfo.getSName();
            //注册信息步骤完成
            welcomeProgressService.addProgress(sNumber);
            //存学号
            sNumberUtils.setSNumber(sNumber);
            sNumberUtils.setSName(sName);
            log.error(sNumberUtils.getSNumber());
            log.error(sNumberUtils.getSName());
            //加一条宿舍信息
            dormitoryallocationService.addRoom(sNumber,sName);
            return Result.ok();
        }else if (s==0){
            return Result.error("学生信息已存在");
        }
        return Result.error("注册失败!");
    }

    //根据学号查询学生信息
    @Operation(summary = "根据学号查询学生信息")
    @GetMapping("/getInfoBySNumber")
    public Result getInfoBySNumber(String sNumber) {
        Studentinfo studentinfo = studentinfoService.getInfoBySNumber(sNumber);
        if (studentinfo != null) {
            return Result.ok(studentinfo);
        }
        return Result.error("暂未查到您的信息!");
    }

    //分页查询学生信息
    @Operation(summary = "分页条件查询")
    @GetMapping("/page")
    public Result page(@RequestParam(defaultValue = "1") Integer pageNumber,
                       @RequestParam(defaultValue = "2") Integer pageSize,
                       String sNumber, String major, String classes) {
        Page<Studentinfo> page = studentinfoService.myPage(pageNumber, pageSize,
                sNumber, major, classes);
        return Result.ok(page);
    }

    //统计学生信息男女人数
    @Operation(summary = "统计学生信息男女人数")
    @GetMapping("/GenderOfStudentCount")
    public Result GenderOfStudentCount() {
        List<GenderOfStudentCount> end = studentinfoService.GenderOfStudentCount();
        return end.isEmpty() ? Result.error("数据丢失") : Result.ok(end);
    }
}

